package org.osgifx;

import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import java.util.HashMap;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextBox;
import javafx.scene.control.Button;
import javafx.scene.Group;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;

import org.osgifx.ui.*;
import org.osgifx.view.*;




/**
 * @author johan
 */



  public var eventListBox: VBox = VBox { };

var eventScreen: Group = Group {content: eventListBox};

var bundleOverviewScreen: BundleOverview = BundleOverview {}

public var applicationOverviewScreen: ApplicationOverview = ApplicationOverview{}

//public var serviceWindow: HBox = HBox {
  //translateY:150;
//}


def WIDTH = 800;
def HEIGHT = 600;
def CONTROL_HEIGHT = 100;

var mainPanel: MainPanel = MainPanel {
  width: WIDTH-8;
  height: HEIGHT - CONTROL_HEIGHT - 20;
}

var controlPanel: ControlPanel = ControlPanel {
  translateY: HEIGHT-CONTROL_HEIGHT-30;
  width: WIDTH-8;
  height: CONTROL_HEIGHT;
}

var addBundleScreen: AddBundleScreen;

public function showAddBundleScreen () {
  mainPanel.active = addBundleScreen;
}

public function showBundleOverviewScreen () {
  mainPanel.active = bundleOverviewScreen;
}

public function showEventScreen () {
  mainPanel.active = eventScreen;
}

public function showApplicationOverviewScreen() {
  mainPanel.active = applicationOverviewScreen;
}

public function showApplication (app:JavaFxView) {
  mainPanel.active = app.getRootNode();
}



var stage: Stage = Stage {
    title: "OSGiFX Service Browser"
    width: WIDTH
    height: HEIGHT
    scene: Scene {
        content: bind [
          mainPanel,
          controlPanel,
          //  installGroup,
        //    bundleOverview,
          //  serviceWindow,
         //   eventListBox
        ]
    }
  }

var bc: BundleContext;
var listener: FxServiceListener;

function run () {
  java.lang.System.setSecurityManager(null);
  stage;
  var knopflerfish: org.knopflerfish.framework.Main =
  new org.knopflerfish.framework.Main();
  var factory:FrameworkFactory = knopflerfish.getFrameworkFactory();
  var map:HashMap = new HashMap();
  map.put("org.osgi.framework.storage.clean", "onFirstInit");
  map.put("org.osgi.framework.system.packages.extra","com.sun.javafx.runtime,org.osgifx.view,javafx.scene,javafx.lang,javafx.scene.paint,com.sun.javafx.runtime.sequence,com.sun.javafx.runtime.location,javafx.scene.shape,javafx.scene.text");
  map.put("org.knopflerfish.startlevel.use", "false");


  var framework: Framework = factory.newFramework(map);
  framework.init();
  bc = framework.getBundleContext();
  listener = FxServiceListener{
    bundleOverview: bundleOverviewScreen;
    bundleContext: bc;
  };
  var filter = "({Constants.OBJECTCLASS}=org.osgifx.view.JavaFxView)";
  bc.addServiceListener (listener, filter);
  bc.addBundleListener (listener);
  addBundleScreen  = AddBundleScreen { bc:bc};
}
